/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.wdm.parsers;

import au.com.bytecode.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;
import org.wdm.beans.Bus;
import org.wdm.beans.BusStop;
import org.wdm.beans.Crime;
import org.wdm.beans.Route;
import org.wdm.beans.points.Point;
import org.wdm.hibernateUtils.HibernateUtil;

/**
 *
 * @author thanasis
 */
public class TransportRoute {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
//        try {
//            HashMap<String, BusStop> busStopMap = new HashMap<String, BusStop>();
//            HashMap<String, Bus> busMap = new HashMap<String, Bus>();
//            CSVReader reader = new CSVReader(new FileReader("/home/thanasis/Desktop/streamNEW.csv"));
//
//            reader.readNext();
//            String[] nextLine;
//            while ((nextLine = reader.readNext()) != null) {
//                String route = nextLine[0];
//                String direction = nextLine[1];
//                String naptan = nextLine[2];
//                String name = nextLine[3];
//                String latitude = nextLine[4];
//                String longitute = nextLine[5];
//                if (!busStopMap.containsKey(naptan)) {
//                    BusStop busStop = new BusStop();
//                    busStop.setNaptan(naptan);
//                    busStop.setName(name);
//                    Point point = new Point();
//                    point.setLatitude(Double.parseDouble(latitude));
//                    point.setLongitude(Double.parseDouble(longitute));
//                    busStop.setPoint(point);
//                    busStopMap.put(naptan, busStop);
//                }
//                Bus bus = null;
//                if (!busMap.containsKey(route)) {
//                    bus = new Bus();
//                    bus.setRoute(route);
//                    busMap.put(route, bus);
//                } else {
//                    bus = busMap.get(route);
//                }
//                if (direction.equals("1")) {
//                    bus.addToGoing(busStopMap.get(naptan));
//                } else {
//                    bus.addToReturning(busStopMap.get(naptan));
//                }
//                busStopMap.get(naptan).addBus(bus);
//            }
//
////            DijkstraCalc.makeGraph(busStopMap, busMap);
//            Point start = new Point(51.51204030,-0.13979350);
//            Point end = new Point(51.50788030,-0.12800410);
////            TreeSet<Route> routesList = RouteCalculation.calculateRoute(busStopMap, busMap, start, end, 0.3);
////            for(Route route : routesList){
////                System.out.println(route);
////            }
//            
//        } catch (IOException ex) {
//            Logger.getLogger(TransportRoute.class.getName()).log(Level.SEVERE, null, ex);
//        }
        
        String userName = "mumem50";
        String password = "9995b06e284bd8b35730f631f38e29f5";
//        String date = request.getParameter("date");
        String date = "";
        int k = 1;
        Set<String> crimeIdSet = new HashSet<String>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        for (double i = 51.281318; i <= 51.676031; i += 0.04) {
            for (double j = -0.543000; j <= 0.270360; j += 0.04) {
                List<Crime> crimeList = PoliceParser.parse(i, j, date, "all-crimes", userName, password);
                for (Crime crime : crimeList) {
                    if(!crimeIdSet.contains(crime.getId())){
                        session.save(crime);
                        crimeIdSet.add(crime.getId());
                    }
                }
                System.out.println("Point Registered: " + k++ + " point: " + j + ", " +i + " crime #: " + crimeList.size());
            }
        }
        for (double i = 51.301318; i <= 51.656031; i += 0.04) {
            for (double j = -0.523000; j <= 0.250360; j += 0.04) {
                List<Crime> crimeList = PoliceParser.parse(i, j, date, "all-crimes", userName, password);
                for (Crime crime : crimeList) {
                    if(!crimeIdSet.contains(crime.getId())){
                        session.save(crime);
                        crimeIdSet.add(crime.getId());
                    }
                }
                System.out.println("Point Registered: " + k++ + " point: " + j + ", " +i + " crime #: " + crimeList.size());
            }
        }
        System.out.println("Total crimes #: " + crimeIdSet.size());
        session.getTransaction().commit();
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}
